﻿using System;
using UnityEditor;
using UnityEngine;

namespace hxweditor
{
	public class BaseEditorWindow<WindowType> : EditorWindow where WindowType: EditorWindow
	{

		public static WindowType window {
			get;
			private set;
		}

		public static bool IsOpen {
			get {
				return window != null;
			}
		}

		public static void OpenWindow ()
		{
			if (IsOpen)
				return;
			window = EditorWindow.GetWindow<WindowType> ();
			window.Show ();
		}

		public static void CloseWindow ()
		{
			if (window != null) {
				window.Close ();
				window = null;
			}
		}

		public static void FocusThis ()
		{
			if (IsOpen)
				window.Focus ();
		}

		//unity3d 提供的功能性方法 比如显示日志，显示进度，压缩纹理，等 见 EditorUtility方法

		//公共的可以重用的画UI的方法  GUILayout和EditorGUILayout中有许多功能相似的方法
		//静态文本
		public static void ShowLabelStatic (string content, GUILayoutOption[] option = null)
		{
			EditorGUILayout.LabelField (content, option);
		}

		//输入文本
		public static string ShowInputString (string title, string content, GUILayoutOption[] option = null)
		{
			return EditorGUILayout.TextField (title, content, option);
		}

		//输入int
		public static int ShowInputInt (string title, int val, GUILayoutOption[] option = null)
		{
			return EditorGUILayout.IntField (title, val, option);
		}

		//输入float
		public static float ShowInputFloat (string title, float val, GUILayoutOption[] option = null)
		{
			return EditorGUILayout.FloatField (title, val, option);
		}

		//按钮
		public static bool ShowButton (string title, GUILayoutOption[] option = null)
		{
			return GUILayout.Button (title, option);
		}

		//显示一个popup列表
		public static Enum ShowPopUp (string title, Enum selected, GUILayoutOption[] option = null)
		{
			return EditorGUILayout.EnumPopup (title, selected, option);
		}

		//空行
		public static void ShowSpaceLine (int rowNum)
		{
			for (int i = 0; i < rowNum; i++) {
				GUILayout.Space (10);//这个可以自定义像素
				//EditorGUILayout.Space();//这个可能是文字的一行
			}
		}

		//显示一个alert
		public static bool ShowAlert (string content)
		{
			return EditorUtility.DisplayDialog ("提示信息", content, "确定", "取消");
		}

		//显示一个进度
		public static void ShowProgress (string title, float current, float total)
		{
			float progress = current / total;
			if (progress >= 1)
				EditorUtility.ClearProgressBar ();
			else
				EditorUtility.DisplayProgressBar (title, string.Format ("当前进度{0}/{1}", current, total), progress);
		}

	}
}

